package cn.wolfcode.web.controller;

import cn.wolfcode.enums.ConsumptionReportEnum;
import cn.wolfcode.qo.ConsumptionReportQueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionReportService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author
 * @Date 2021/1/17 13:21
 * @Version 1.0
 * @Name:lzh
 **/
@Controller
@RequestMapping("/consumptionReport")
public class ConsumptionReportController {
    @Autowired
    private IConsumptionReportService consumptionReportService;
    @Autowired
    private IBusinessService businessService;

    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") ConsumptionReportQueryObject qo){
        PageInfo<HashMap> pageInfo = consumptionReportService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("business",businessService.listAll());
        model.addAttribute("enum", ConsumptionReportEnum.values());
        return "/consumptionReport/list";
    }

    @RequestMapping("/listBar")
    public String listBar(Model model,@ModelAttribute("qo") ConsumptionReportQueryObject qo){
        qo.setPageSize(0);
        List<HashMap> list = consumptionReportService.query(qo).getList();
        ////①准备5个容器
        ArrayList<Object> xList=new ArrayList<>();
        ArrayList<Object> numberList=new ArrayList<>();
        ArrayList<Object> totalAmountList=new ArrayList<>();
        ArrayList<Object> discountAmountList=new ArrayList<>();
        ArrayList<Object> payAmountList=new ArrayList<>();
        ////②遍历查询出来的集合,获取到每个map中
        for (HashMap map:list){
            //③根据不同的key获取value放到不同的容器中
            xList.add(map.get("groupType"));
            numberList.add(map.get("totalCount"));
            totalAmountList.add(map.get("totalAmount"));
            discountAmountList.add(map.get("discountAmountList"));
            payAmountList.add(map.get("payAmount"));
        }
        String s = JSON.toJSONString(xList);


        model.addAttribute("xList", s);
        model.addAttribute("numberList",JSON.toJSONString(numberList));
        model.addAttribute("totalAmountList",JSON.toJSONString(totalAmountList));
        model.addAttribute("discountAmountList",JSON.toJSONString(discountAmountList));
        model.addAttribute("payAmountList",JSON.toJSONString(payAmountList));
        return "consumptionReport/listBar";
    }
}
